﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace NCKH.Personal
{
    public partial class Thuyetminhdetai : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hien();
                //hientrangthai();
                hiendetai();
                hiennhomthuchien();
                CKEditorphuongphap();
                CKEditorsanpham();
            }
        }
        protected void CKEditorphuongphap()
        {
            CKEphuongphap.config.toolbar = new object[] { 
                
         new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, 
         new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat" }, 
         new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" }, 
         new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
         new object[] { "Styles", "Format", "Font", "FontSize" }};

        }

        protected void CKEditorsanpham()
        {
            CKEsanpham.config.toolbar = new object[] { 
                
         new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, 
         new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat" }, 
         new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" }, 
         new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
         new object[] { "Styles", "Format", "Font", "FontSize" }};

        }
        private void hiendetai()
        {
            string sqlSelect = "SELECT MaDT, TenDT"
                    + " FROM DETAI where Trangthai=N'Đề xuất mới'";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    //Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddlTenDT.Items.Insert(0, "-- Chọn --");
                        while (Rd.Read() && Rd.HasRows)
                        {
                            ddlTenDT.Items.Add(
                                new ListItem(Rd["TenDT"].ToString(),
                                    Rd["MaDT"].ToString()));

                        }

                        Rd.Close();
                    }
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        private void hiennhomthuchien()
        {
            string sqlSelectcap = "SELECT Manhom, Tennhom"
                    + " FROM NHOMTHUCHIEN ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelectcap, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddlTenN.Items.Insert(0, "--Chọn--");
                        while (Rd.Read() && Rd.HasRows)
                        {
                            ddlTenN.Items.Add(
                                new ListItem(Rd["Tennhom"].ToString(),
                                    Rd["Manhom"].ToString()));
                        }

                        Rd.Close();
                    }

                    Cnn.Close();
                }
            }
        }

        protected void hien()
        {
            using (DataTable THONGTINTHUYETMINHDT = getThongtinTMDT())
            {
                grvThongtinTM.DataSource = THONGTINTHUYETMINHDT;
                grvThongtinTM.DataBind();
            }
        }
        DataTable getThongtinTMDT()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Select_ThongtinTM", Cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable("THONGTINTHUYETMINHDT");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }
        protected void ThemTM()
        {
            DateTime ngaynop = Convert.ToDateTime(txtngaynopTM.Text);
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            SqlConnection cnn = new SqlConnection(connectionString);
            using (SqlCommand cmd = new SqlCommand("Themthuyetminh", cnn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MaDT", ddlTenDT.SelectedValue);
                cmd.Parameters.AddWithValue("@Manhom", ddlTenN.SelectedValue);
                cmd.Parameters.AddWithValue("@NgaynopbanTM",ngaynop);
                cmd.Parameters.AddWithValue("@PPtiepcanNC", CKEphuongphap.Text);
                cmd.Parameters.AddWithValue("@Dukiensanpham", CKEsanpham.Text);
                cmd.Parameters.AddWithValue("@Dukiendiachiungdung ", txtdiadiemUD.Text);
                cmd.Parameters.AddWithValue("@KinhphithuchienDT", txtkinhphi.Text);
                cmd.Parameters.AddWithValue("@Trangthai", " Chưa duyệt");
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }

        }
        protected void Capnhatdetai()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            SqlConnection cnn = new SqlConnection(connectionString);
            using (SqlCommand cmd = new SqlCommand("Capnhat_detai", cnn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MaDT", ddlTenDT.SelectedValue);
                cmd.Parameters.AddWithValue("@Dukiendiachiungdung ", txtdiadiemUD.Text);
                cmd.Parameters.AddWithValue("@Trangthai", " Chưa duyệt");
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
        }
        protected void ibtThem_Click(object sender, EventArgs e)
        {
            ThemTM();
            Capnhatdetai();
            hien();
        }




        protected void grvThongtinTM_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int thuyetminhID = Convert.ToInt32(e.CommandArgument);

                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Xoathuyetminh", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("@MaTM", thuyetminhID);

                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hien();
                    }
                }
            }
        }

        protected void grvThongtinTM_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvThongtinTM.PageIndex = e.NewPageIndex;
            hien();

        }

        protected void cldngaynopTM_SelectionChanged(object sender, EventArgs e)
        {
            txtngaynopTM.Text = cldngaynopTM.SelectedDate.ToShortDateString();
        }


        protected void ibtHuy_Click(object sender, EventArgs e)
        {
            txtngaynopTM.Text =
            CKEphuongphap.Text =
            CKEsanpham.Text =
            txtdiadiemUD.Text =
            txtkinhphi.Text =
            string.Empty;
            txtngaynopTM.Focus();

        }

    }
}